package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.mingtu.bio.bean.AnimalResDetails;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.jgspatrol.R;

import java.util.List;

public class CardPagerAdapter extends PagerAdapter {

    private List<AnimalResDetails.ResAllBean.ResAllPicsBean> mData;
    private Context context;

    public CardPagerAdapter(Context context, List<AnimalResDetails.ResAllBean.ResAllPicsBean> date) {
        mData = date;
        this.context = context;
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.layout_card_view, container, false);
        container.addView(view);
        bind(mData.get(position), view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);

    }

    private void bind(AnimalResDetails.ResAllBean.ResAllPicsBean resAllPicsBean, View view) {
        ShapeableImageView ivPicture = (ShapeableImageView) view.findViewById(R.id.iv_picture);
        String url = resAllPicsBean.getUrl();
        Glide.with(context)
                .load(url)
                .apply(BaseApplication.getRequestOptions())
                .into(ivPicture);
    }

}
